<?php

class Core_Model_Road_Info extends Dcmp_Model_Spoor {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_road_info');
    }

    public function getLandmarks() {
        if (!$this->hasData('landmarks')) {
            $this->data['landmarks'] = App::getModel('foundation/collection_poi')->contain('a.id', ':ids:s')
                            ->addParameter('ids', $this->getLandmarkIds())->load();
        }
        return $this->data['landmarks'];
    }

    public function getRecommendPois() {
        if (!$this->hasData('recommend_pois')) {
            $this->data['recommend_pois'] = App::getModel('foundation/collection_poi')->contain('a.id', ':ids:s')
                            ->addParameter('ids', $this->getRecommendPoiIds())->load();
        }
        return $this->data['recommend_pois'];
    }

    public function getRecommendConcessions() {
        if (!$this->hasData('recommend_concessions')) {
            $this->data['recommend_concessions'] = App::getModel('customer/collection_concessions')->contain('a.id', ':ids:s')
                            ->addParameter('ids', $this->getRecommendConcessionsIds())->load();
        }
        return $this->data['recommend_concessions'];
    }

    protected function insertSelf($transaction) {
        $data = $this->getAllData();
        if (is_array($data['recommend_poi_ids'])) {
            $data['recommend_poi_ids'] = implode(',', $data['recommend_poi_ids']);
        }
        if (is_array($data['landmark_ids'])) {
            $data['landmark_ids'] = implode(',', $data['landmark_ids']);
        }
        if (is_array($data['recommend_concessions_ids'])) {
            $data['recommend_concessions_ids'] = implode(',', $data['recommend_concessions_ids']);
        }
        $this->getTable()->insert($data, $transaction);
        $this->setId($this->getTable()->getInsertId());
        $this->getTable()->insertRelation($this->getCode(), $this->getLandmarkIds(), $transaction);
    }

    protected function afterInsert($transaction) {
        if ($this->getFeature()) {
            App::getHelper('global')->registerKeywords(explode(' ', $this->getFeature()), $this->getCode(), 'road');
        }
    }

    protected function updateSelf($transaction) {
        $data = $this->getChangedData();
        if (isset($data['recommend_poi_ids']) && is_array($data['recommend_poi_ids'])) {
            $data['recommend_poi_ids'] = implode(',', $data['recommend_poi_ids']);
        }
        if (isset($data['landmark_ids']) && is_array($data['landmark_ids'])) {
            $data['landmark_ids'] = implode(',', $data['landmark_ids']);
        }
        if (isset($data['recommend_concessions_ids']) && is_array($data['recommend_concessions_ids'])) {
            $data['recommend_concessions_ids'] = implode(',', $data['recommend_concessions_ids']);
        }
        $data['id'] = $this->getId();
        $this->getTable()->update($data, $transaction);

        if ($this->propertyIsChanged('landmark')) {
            $this->getTable()->deleteRelation($this->getCode(), $transaction);
            $this->getTable()->insertRelation($this->getCode(), $this->getLandmark(), $transaction);
        }
    }

    protected function afterUpdate($transaction) {
        if ($this->propertyIsChanged('feature')) {
            App::getHelper('global')->updateKeywords(explode(' ', $this->getOriginalData('feature')), explode(' ', $this->getFeature()), $this->getCode(), 'road');
        }
    }

    public function fill($data) {
        if (array_key_exists('recommend_poi_ids', $data) && !is_array($data['recommend_poi_ids'])) {
            $data['recommend_poi_ids'] = explode(',', $data['recommend_poi_ids']);
        }
        if (array_key_exists('recommend_concessions_ids', $data) && !is_array($data['recommend_concessions_ids'])) {
            $data['recommend_concessions_ids'] = explode(',', $data['recommend_concessions_ids']);
        }
        if (array_key_exists('landmark_ids', $data) && !is_array($data['landmark_ids'])) {
            $data['landmark_ids'] = explode(',', $data['landmark_ids']);
        }
        return parent::fill($data);
    }

    protected function initializeTable($table) {
        $table->setName('{t}road_info');
        $table->createColumn('id', 'string', TRUE, TRUE);
        $table->createColumn('code', 'string');
        $table->createColumn('alias', 'string');
        $table->createColumn('detail', 'string');
        $table->createColumn('precinct', 'string');
        $table->createColumn('landmark_ids', 'string');
        $table->createColumn('feature', 'string');
        $table->createColumn('recommend_concessions_ids', 'string');
        $table->createColumn('recommend_poi_ids', 'string');
        $table->createColumn('traffic', 'string');
        $table->createColumn('status', 'string')->setDefaultValue('draft');
        parent::initializeTable($table);
    }

    public function loadByCode($code) {
        $row = $this->getTable()->getByCode($code);
        return $this->fill($row);
    }

}